package cn.xej.easyexcel.export.impl;

import cn.xej.easyexcel.common.BaseQuery;
import cn.xej.easyexcel.common.RespObj;
import cn.xej.easyexcel.export.AbstractExportService;
import cn.xej.easyexcel.export.excelcolumn.UserColumn;
import cn.xej.easyexcel.pojo.User;
import cn.xej.easyexcel.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author <a href="mailto:enjie.xu@yunhutech.com">xiaoming</a>
 * @since 2021/6/7
 *
 * 用户导出excel实现类
 */
@Service("exportUserService")
public class ExportUserService extends AbstractExportService<BaseQuery> {

    @Resource
    private UserService userService;

    @Override
    public Class<BaseQuery> paramClass() {
        return BaseQuery.class;
    }

    @Override
    public String title() {
        return "用户记录";
    }

    @Override
    public String makefile(BaseQuery request) {
        System.out.println(request);
        RespObj obj = userService.getUsers();

        List<UserColumn> dataList = new ArrayList<>();
        List<User> userList = (List<User>) obj.getData();
        userList.forEach(u -> {
            UserColumn column = new UserColumn();
            BeanUtils.copyProperties(u,column);
            dataList.add(column);
        });

        return super.writeExcel(UserColumn.class,dataList);
    }
}
